package nc.impl.hrppl.pjapplication.pjappl;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.itf.hrpbm.sjxm.sjxmh.ISjxmHVOService;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.hrppl.pjapplication.AggPjapplVO;
import nc.vo.hrppl.pjapplication.PJApplHVO;
import nc.vo.hrppl.pjapplication.PjapplPVO;
import nc.vo.hrppl.proupdate.AggProUpdateHVO;
import nc.vo.hrppl.proupdate.CGUpdateCVO;
import nc.vo.hrppl.proupdate.ProUpdateHVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.sjxm.sjxm.AggSjxmVO;
import nc.vo.sjxm.sjxm.SjxmBVO;
import nc.vo.sjxm.sjxm.SjxmHVO;
import org.apache.commons.lang3.ArrayUtils;
import java.io.*;
import java.util.*;

public class SJXMUtil {
    private ISjxmHVOService sjxmHVOService;
    private BaseDAO baseDAO;

    /**
     * 四级项目增加 提交回写采购四级项目
     * @param vos
     * @throws BusinessException
     */
     public static void commitAggProUpdateHVO(AggProUpdateHVO[] vos) throws BusinessException{
         if(ArrayUtils.isNotEmpty(vos)){
             SJXMUtil util = new SJXMUtil();
             for (AggProUpdateHVO aggvo : vos) {
                 ProUpdateHVO hvo = aggvo.getParentVO();
                 String applyyear = hvo.getApplyyear();
                 String pk_proapp = hvo.getPk_proapp();
                 ISuperVO[] bvos = aggvo.getChildren(CGUpdateCVO.class);
                 List<Map<String, Object>> plists = util.getParentProject(pk_proapp);
                 List<Object> list = new ArrayList<>();
                 if(ArrayUtils.isNotEmpty(bvos)){
                     for (ISuperVO bvo : bvos) {
                         Object isorigin_c = bvo.getAttributeValue("isorigin_c"); // 是否原始行
                         if(!"Y".equals(isorigin_c+"")){
                             Object vbdef1 = bvo.getAttributeValue("vbdef1"); // 四级项目
                             Object vbdef2 = bvo.getAttributeValue("vbdef2") == null ? applyyear : bvo.getAttributeValue("vbdef2"); // 预算年度
                             list.add(vbdef1+"_"+vbdef2);
                         }

                     }
                     
                 }
                 if(!list.isEmpty()){
                     for (Object o : list) {
                         String[] s = o.toString().split("_");
                         String pk = s[0];
                         String year = s[1];
                         AggSjxmVO sjaggvo = util.getSjxmHVOService().findAggSjxmVOByPk(pk);
                         if(sjaggvo != null){
                             SjxmHVO hhvo = sjaggvo.getParentVO();
                             hhvo.setStatus(VOStatus.UPDATED);
                             ISuperVO[] sjbvos = sjaggvo.getChildren(SjxmBVO.class);
                             if(ArrayUtils.isNotEmpty(sjbvos)){
                                 for (ISuperVO sjbvo : sjbvos) {
                                     Object ysyear = sjbvo.getAttributeValue("ysyear");
                                     if(year.equals(ysyear.toString())){
                                         sjbvo.setStatus(VOStatus.UPDATED);
                                         sjbvo.setAttributeValue("pk_group",hvo.getPk_group());
                                         sjbvo.setAttributeValue("pk_org",hvo.getPk_org());
                                         sjbvo.setAttributeValue("pk_org_v",hvo.getPk_org_v());
                                         sjbvo.setAttributeValue("bdef1","Y");
                                         sjbvo.setAttributeValue("modifyobj","HOP");
                                         sjbvo.setAttributeValue("budstate","2");//  已审批=1,初始态=2,未审批=3 初始态=1,项目已打包=2,采购已安排=3,终止采购=4,结项=5
                                         sjbvo.setAttributeValue("fourstate","2");
                                         for (Map<String, Object> plist : plists) {
                                             sjbvo = util.setSJCGXM(sjbvo, plist);
                                         }

                                     }


                                 }


                             }


                         }
                         util.getSjxmHVOService().saveAggSjxmVO(sjaggvo);


                     }


                 }
                 
             }
             
         }
         
     }

    /**
     * 设置四级项目
     * @param sjbvo
     * @param map
     * @return
     * @throws BusinessException
     */
     public ISuperVO setSJCGXM(ISuperVO sjbvo, Map<String, Object> map) throws BusinessException{
         Object leve = map.get("leve");
         Object project_code = map.get("project_code");
         Object project_name = map.get("project_name");
         if("1".equals(map.get("leve").toString())){
             sjbvo.setAttributeValue("oneprono",project_code);
             sjbvo.setAttributeValue("oneproname",project_name);
         }else if("2".equals(map.get("leve").toString())){
             sjbvo.setAttributeValue("twoprono",project_code);
             sjbvo.setAttributeValue("twoproname",project_name);
         }else if("3".equals(map.get("leve").toString())){
             sjbvo.setAttributeValue("threeprono",project_code);
             sjbvo.setAttributeValue("threeproname",project_name);
         }

         return sjbvo;
     }
    /**
     * 获取项目级次数据
     * @param pk_project
     * @return
     * @throws BusinessException
     */
     protected List<Map<String, Object>> getParentProject(Object pk_project) throws BusinessException{
         List<Map<String, Object>> o = (List<Map<String, Object>>)this.getBaseDAO().executeQuery(this.buildParProject(pk_project), new MapListProcessor());
         return o == null || o.size() < 0 ? null : o;
     }
    /**
     * 四级项目取消提交收回回写采购四级项目
     * @param vos
     * @throws BusinessException
     */
    public static void UnCommitAggProUpdateHVO(AggProUpdateHVO[] vos) throws BusinessException{
        if(ArrayUtils.isNotEmpty(vos)){
            SJXMUtil util = new SJXMUtil();
            for (AggProUpdateHVO aggvo : vos) {
                ProUpdateHVO hvo = aggvo.getParentVO();
                String applyyear = hvo.getApplyyear();
                String pk_proapp = hvo.getPk_proapp();
                ISuperVO[] bvos = aggvo.getChildren(CGUpdateCVO.class);
                List<Map<String, Object>> plists = util.getParentProject(pk_proapp);
                List<Object> list = new ArrayList<>();
                if(ArrayUtils.isNotEmpty(bvos)){
                    for (ISuperVO bvo : bvos) {
                        Object isorigin_c = bvo.getAttributeValue("isorigin_c"); // 是否原始行
                        if(!"Y".equals(isorigin_c+"")){
                            Object vbdef1 = bvo.getAttributeValue("vbdef1"); // 四级项目
                            Object vbdef2 = bvo.getAttributeValue("vbdef2") == null ? applyyear : bvo.getAttributeValue("vbdef2"); // 预算年度
                            list.add(vbdef1+"_"+vbdef2);
                        }

                    }

                }
                if(!list.isEmpty()){
                    for (Object o : list) {
                        String[] s = o.toString().split("_");
                        String pk = s[0];
                        String year = s[1];
                        AggSjxmVO sjaggvo = util.getSjxmHVOService().findAggSjxmVOByPk(pk);
                        if(sjaggvo != null){
                            SjxmHVO hhvo = sjaggvo.getParentVO();
                            hhvo.setStatus(VOStatus.UPDATED);
                            ISuperVO[] sjbvos = sjaggvo.getChildren(SjxmBVO.class);
                            if(ArrayUtils.isNotEmpty(sjbvos)){

                                for (ISuperVO sjbvo : sjbvos) {

                                    Object ysyear = sjbvo.getAttributeValue("ysyear");
                                    if(year.equals(ysyear.toString())){
                                        sjbvo.setStatus(VOStatus.UPDATED);
                                        sjbvo.setAttributeValue("pk_group",hvo.getPk_group());
                                        sjbvo.setAttributeValue("pk_org",hvo.getPk_org());
                                        sjbvo.setAttributeValue("pk_org_v",hvo.getPk_org_v());
                                        sjbvo.setAttributeValue("bdef1","N");
                                        sjbvo.setAttributeValue("modifyobj","HOP");
                                        sjbvo.setAttributeValue("budstate","2");//  已审批=1,初始态=2,未审批=3 初始态=1,项目已打包=2,采购已安排=3,终止采购=4,结项=5
                                        sjbvo.setAttributeValue("fourstate","1");
                                        sjbvo.setAttributeValue("kymoney",new UFDouble(0));
                                        sjbvo.setAttributeValue("threeprofinysmoney",new UFDouble(0));
                                        sjbvo.setAttributeValue("threeproysmoney",new UFDouble(0));
                                        sjbvo.setAttributeValue("threeprotzmoney",new UFDouble(0));
                                        for (Map<String, Object> plist : plists) {
                                            sjbvo = util.setSJCGXM(sjbvo, plist);
                                        }

                                    }


                                }


                            }


                        }
                        util.getSjxmHVOService().saveAggSjxmVO(sjaggvo);


                    }


                }

            }

        }

    }

    /**
     * 四级项目审批通过
     * @param vos
     * @throws BusinessException
     */
    public static void callbackAPPROVEAggProUpdateHV(AggProUpdateHVO[] vos) throws BusinessException {
        if(ArrayUtils.isNotEmpty(vos)){
            SJXMUtil util = new SJXMUtil();
            for (AggProUpdateHVO aggvo : vos) {
                ProUpdateHVO hvo = aggvo.getParentVO();
                String applyyear = hvo.getApplyyear();
                String pk_proapp = hvo.getPk_proapp();
                String pk_bugetdept = hvo.getChargedept();
                UFDouble zmonery = hvo.getUpdallfun();// 调整后的总经经费
                UFDouble zbmonery = hvo.getUpdbeforefun(); // 项目总经费
                UFDouble tzmonery = zmonery.sub(zbmonery);
                ISuperVO[] bvos = aggvo.getChildren(CGUpdateCVO.class);
                List<Map<String, Object>> plists = util.getParentProject(pk_proapp);
                List<Object> list = new ArrayList<>();
                if(ArrayUtils.isNotEmpty(bvos)){
                    for (ISuperVO bvo : bvos) {
                        Object vbdef1 = bvo.getAttributeValue("vbdef1"); // 四级项目
                        Object vbdef2 = bvo.getAttributeValue("vbdef2") == null ? applyyear : bvo.getAttributeValue("vbdef2"); // 预算年度
                        list.add(vbdef1+"_"+vbdef2);

                    }

                }
                if(!list.isEmpty()){
                    for (Object o : list) {
                        String[] s = o.toString().split("_");
                        String pk = s[0];
                        String year = s[1];
                        AggSjxmVO sjaggvo = util.getSjxmHVOService().findAggSjxmVOByPk(pk);
                        if(sjaggvo != null){

                            SjxmHVO hhvo = sjaggvo.getParentVO();
                            hhvo.setStatus(VOStatus.UPDATED);
                            ISuperVO[] sjbvos = sjaggvo.getChildren(SjxmBVO.class);
                            if(ArrayUtils.isNotEmpty(sjbvos)){

                                for (ISuperVO sjbvo : sjbvos) {
                                    Object ysyear = sjbvo.getAttributeValue("ysyear");
                                    if(year.equals(ysyear.toString())){
                                        List<String> bblists = (List<String>)util.getBaseDAO().executeQuery(util.buildBudgetMakeBBSQL(year, pk_bugetdept, pk_proapp), new ColumnListProcessor());
                                        String budstate ="2", fourstate = "2";
                                        if(bblists != null && !bblists.isEmpty()){ // 已本年度已编制
                                            budstate = "1";
                                            fourstate = "2";
                                        }
                                        sjbvo.setStatus(VOStatus.UPDATED);
                                        sjbvo.setAttributeValue("modifyobj","HOP");
                                        sjbvo.setAttributeValue("bdbilldate",new UFDate());
                                        sjbvo.setAttributeValue("budstate",budstate);//  已审批=1,初始态=2,未审批=3 初始态=1,项目已打包=2,采购已安排=3,终止采购=4,结项=5
                                        sjbvo.setAttributeValue("fourstate",fourstate);
                                        sjbvo.setAttributeValue("kymoney",zmonery);
                                        sjbvo.setAttributeValue("threeprofinysmoney",zmonery);
                                        sjbvo.setAttributeValue("threeproysmoney",zmonery);
                                        UFDouble tz = sjbvo.getAttributeValue("threeprotzmoney") == null ? new UFDouble(0) : new UFDouble(sjbvo.getAttributeValue("threeprotzmoney").toString());
                                        sjbvo.setAttributeValue("threeprotzmoney",tzmonery.add(tz));
                                        for (Map<String, Object> plist : plists) {
                                            sjbvo = util.setSJCGXM(sjbvo, plist);
                                        }

                                    }


                                }


                            }


                        }
                        if(sjaggvo != null){
                            util.getSjxmHVOService().saveAggSjxmVO(sjaggvo);
                            util.getBaseDAO().executeUpdate("commit");
                        }


                    }


                }

            }

        }

    }

    /**
     * 四级项目取消审批回写采购四级项目
     * @param vos
     * @throws BusinessException
     */
    public static void callbackUNAPPROVEAggProUpdateHV(AggProUpdateHVO[] vos) throws BusinessException{
        if(ArrayUtils.isNotEmpty(vos)){
            SJXMUtil util = new SJXMUtil();
            for (AggProUpdateHVO aggvo : vos) {
                ProUpdateHVO hvo = aggvo.getParentVO();
                String applyyear = hvo.getApplyyear();
                String pk_proapp = hvo.getPk_proapp();
                UFDouble zmonery = hvo.getUpdallfun();// 调整后的总经经费
                UFDouble zbmonery = hvo.getUpdbeforefun(); // 项目总经费
                UFDouble tzmonery = zmonery.sub(zbmonery);
                ISuperVO[] bvos = aggvo.getChildren(CGUpdateCVO.class);
                List<Map<String, Object>> plists = util.getParentProject(pk_proapp);
                List<Object> list = new ArrayList<>();
                if(ArrayUtils.isNotEmpty(bvos)){
                    for (ISuperVO bvo : bvos) {
                        Object vbdef1 = bvo.getAttributeValue("vbdef1"); // 四级项目
                        Object vbdef2 = bvo.getAttributeValue("vbdef2") == null ? applyyear : bvo.getAttributeValue("vbdef2"); // 预算年度
                        list.add(vbdef1+"_"+vbdef2);

                    }

                }
                if(!list.isEmpty()){
                    for (Object o : list) {
                        String[] s = o.toString().split("_");
                        String pk = s[0];
                        String year = s[1];
                        AggSjxmVO sjaggvo = util.getSjxmHVOService().findAggSjxmVOByPk(pk);
                        if(sjaggvo != null){

                            SjxmHVO hhvo = sjaggvo.getParentVO();
                            hhvo.setStatus(VOStatus.UPDATED);
                            ISuperVO[] sjbvos = sjaggvo.getChildren(SjxmBVO.class);
                            if(ArrayUtils.isNotEmpty(sjbvos)){

                                for (ISuperVO sjbvo : sjbvos) {

                                    Object ysyear = sjbvo.getAttributeValue("ysyear");
                                    if(year.equals(ysyear.toString())){
                                        sjbvo.setStatus(VOStatus.UPDATED);
                                        sjbvo.setAttributeValue("bdbilldate",null);
                                        sjbvo.setAttributeValue("modifyobj","HOP");
                                        sjbvo.setAttributeValue("budstate","2");//  已审批=1,初始态=2,未审批=3 初始态=1,项目已打包=2,采购已安排=3,终止采购=4,结项=5
                                        sjbvo.setAttributeValue("fourstate","1");
                                        sjbvo.setAttributeValue("kymoney",zbmonery);
                                        sjbvo.setAttributeValue("threeprofinysmoney",zbmonery);
                                        sjbvo.setAttributeValue("threeproysmoney",zbmonery);
                                        UFDouble tz = sjbvo.getAttributeValue("threeprotzmoney") == null ? new UFDouble(0) : new UFDouble(sjbvo.getAttributeValue("threeprotzmoney").toString());
                                        sjbvo.setAttributeValue("threeprotzmoney",tz.sub(tzmonery));
                                        for (Map<String, Object> plist : plists) {
                                            sjbvo = util.setSJCGXM(sjbvo, plist);
                                        }

                                    }


                                }


                            }


                        }
                        util.getSjxmHVOService().saveAggSjxmVO(sjaggvo);


                    }


                }

            }

        }

    }

    /**
     *
     * 获取当前项目是否已经编报完成
     * @param nyear          年度
     * @param pk_budgetdept   部门
     * @param pk_project      项目
     * @return
     * @throws BusinessException
     */
    protected String buildBudgetMakeBBSQL(Object nyear, Object pk_budgetdept ,Object pk_project) throws BusinessException{
        StringBuilder sql = new StringBuilder();
        sql.append(" select \n" );
        sql.append("   h.billno \n" );
        sql.append(" from hrpbm_budgetmake h\n" );
        sql.append(" inner join hrpbm_budgetmakedetail b on h.pk_budgetmake = b.pk_budgetmake\n" );
        sql.append(" inner join hrppl_pjappl hp on hp.pk_pjappl = b.pk_project\n" );
        sql.append(" where h.dr = 0\n" );
        sql.append("   and b.dr = 0\n" );
        sql.append("   and h.taskstate = 99\n" ); // 获取编报完成的
        sql.append("   and b.pk_project = '"+pk_project+"' \n" );
        sql.append("   and b.nyear = '"+nyear+"'\n" );
        sql.append("   -- and b.pk_budgetdept = '"+pk_budgetdept+"'");
        return sql.toString();
    }

    /**
     * 项目修订 删除四级项目回写
     * @param var2       修订前的VO
     * @param vos        修订后的VO
     * @param budstate   预算状态
     * @param fourstate  四级项目状态
     * @param type       操作类型
     * @param nyear      预算年度的
     * @throws BusinessException
     */
    public static void sjxmPjlibcategoryHandlerAggvo(AggPjapplVO var2, AggPjapplVO[] vos,String budstate, String fourstate,String type, Object nyear) throws BusinessException{
        SJXMUtil util = new SJXMUtil();
        if(ArrayUtils.isEmpty(vos)) return ;
        Map<String, String> sjmaps = new HashMap<>(); //  所有需要调整的四级项目汇总 修改、新增、删除
        HashMap<Object, List<Map<String, Object>>> pks = new HashMap<>();
        PJApplHVO hvo = var2.getParentVO();
        PjapplPVO[] ybvos = (PjapplPVO[])var2.getChildren(PjapplPVO.class);
        List<String> ylists =new ArrayList<>();
        if(!ArrayUtils.isEmpty(ybvos)) { // 原项目为空
            String applyyear = hvo.getApplyyear();
            // 修改前的四级项目
            for (PjapplPVO bvo : ybvos) {
                Object vbdef1 = bvo.getAttributeValue("vbdef1");
                Object vbdef2 = bvo.getAttributeValue("vbdef2");
                if(vbdef1 == null) continue;
                if(vbdef2 != null){
                    applyyear = vbdef2.toString();
                }
                String key = vbdef1.toString()+"_"+applyyear;
                ylists.add(key);
            }
//            ylists = Arrays.stream(ybvos).map(vo -> vo.getVbdef1() +"_"+ (vo.getVbdef2() == null ? applyyear : vo.getVbdef2())).collect(Collectors.toList());
        }
        PJApplHVO xhvo = vos[0].getParentVO();
        String xyear = xhvo.getApplyyear();
        String xdept = xhvo.getOfficedept();
        String xpk_pjappl = xhvo.getPk_pjappl();
        PjapplPVO[] zxvos = (PjapplPVO[])vos[0].getChildren(PjapplPVO.class);
        ArrayList<String> zxlists = new ArrayList<>();
        // 修正后的四级项目
        if(!ArrayUtils.isEmpty(zxvos)){
            String applyyear = xhvo.getApplyyear();
            for (PjapplPVO bvo : zxvos) {
                Object vbdef1 = bvo.getAttributeValue("vbdef1");
                Object vbdef2 = bvo.getAttributeValue("vbdef2");
                if(vbdef1 == null) continue;
                if(vbdef2 != null){
                    applyyear = vbdef2.toString();
                }
                String key = vbdef1.toString()+"_"+applyyear;
                zxlists.add(key);
            }
        }
        // 修正后的四级项目
//        List<String> zxlists = Arrays.stream(zxvos).map(vo -> vo.getVbdef1() +"_"+  (vo.getVbdef2() == null ? xyear : vo.getVbdef2())).collect(Collectors.toList());
        for (String ypk : ylists) { // 修正前的四级项目
            if (zxlists.contains(ypk)) { // 修改
                sjmaps.put(ypk, "U");
            }else { // 删除
                sjmaps.put(ypk, "D");
            }
        }
        for (String zxlist : zxlists) { // 修正后的四级项目
            if (ylists.contains(zxlist)) { // 修改
                sjmaps.put(zxlist, "U");
            }else{
                sjmaps.put(zxlist, "I");
            }
        }
        List<Map<String, Object>> listsjs = util.getSJXMToProjectParent(vos[0], budstate, fourstate, type, nyear);
        for (String key : sjmaps.keySet()) {
            List<Map<String, Object>> listMap = util.deepCopy(listsjs);
            String value = sjmaps.get(key);
            String[] s1 = key.split("_");
            String sql = util.buildBudgetMakeSQL(s1[1], xdept, xpk_pjappl);
            Map<String,Object> xmap = (Map<String,Object>)util.getBaseDAO().executeQuery(sql, new MapProcessor());
            if ("D".equals(value)) {
                fourstate = "1";
                budstate = "2";
            }else if("I".equals(value)) {
                UFDouble zzdata = new UFDouble(0);
                UFDouble tzdata = new UFDouble(0);
                HashMap<String, Object> map = new HashMap<>();
                if(xmap != null) { //  当前四级项目对应的单机项目当前年度编报完成
                    zzdata = new UFDouble(xmap.get("amount").toString());
                    tzdata = new UFDouble(xmap.get("tzamount").toString());
                    fourstate = "2";
                    budstate = "1";
                }else {
                    fourstate = "2";
                    budstate = "2";
                }
                map.put("fourstate",fourstate);
                map.put("budstate",budstate);
                map.put("kymoney",zzdata);
                map.put("threeprofinysmoney",zzdata);
                map.put("threeproysmoney",zzdata);
                map.put("threeprotzmoney",tzdata);
                map.put("bdbilldate",new UFDate()); // 已审批=1,初始态=2,未审批=3   初始态=1,项目已打包=2,采购已安排=3,终止采购=4,结项=5
                listMap.add(map);
            }else{
                for (Map<String, Object> m : listMap) {
                    if (m.get("fourstate") != null) {
                        m.put("fourstate", null);
                    }
                    if (m.get("budstate") != null) {
                        m.put("budstate", null);
                    }
                }
            }
            listMap = util.handListMap(listMap, budstate,fourstate,value);
            String[] s = key.split("_");
            pks.put(s[0]+"_spli_"+s[1], listMap);
        }
        if(pks.isEmpty()) return ;
        AggSjxmVO[] aggvos = util.setValueAggSjxmVO(pks, null, null, type);
        util.getSjxmHVOService().saveAggSjxmVO(aggvos);
    }

    /**
     *四级项目最终修改的VO字段值
     * @param lists
     * @return
     * @throws BusinessException
     */
    protected List<Map<String, Object>> handListMap(List<Map<String, Object>> lists,String budstate, String fourstate, String type) throws BusinessException {
        if ("D".equals(type)) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("jsd","jsd");
            map.put("oneprono","");
            map.put("oneproname","");
            map.put("twoprono","");
            map.put("twoproname","");
            map.put("threeprono","");
            map.put("threeproname","");
            if(budstate != null) {map.put("budstate",budstate);}
            if(fourstate != null) {map.put("fourstate",fourstate);}
            map.put("kymoney",new UFDouble(0));
            map.put("threeprofinysmoney",new UFDouble(0));
            map.put("threeprotzmoney",new UFDouble(0));
            map.put("threeproysmoney",new UFDouble(0));
            map.put("bdbilldate", null);
            map.put("modifyobj","");
            lists.add(map);
        }
        return lists;
    }

    /**
     * 获取当前四级项目状态以及父级项目信息
     * @return
     * @throws BusinessException
     */
    protected  List<Map<String, Object>> getSJXMToProjectParent(AggPjapplVO vo ,String budstate, String fourstate,String type, Object nyear)  throws BusinessException {
        SJXMUtil util = new SJXMUtil();
        List<Map<String, Object>> parpros = new ArrayList<Map<String, Object>>();
        PJApplHVO hvo = vo.getParentVO();
        Object belongpro = hvo.getAttributeValue("belongpro");// 上级项目
        Object applyyear =  "".equals(nyear) || nyear == null ? hvo.getAttributeValue("applyyear") : nyear;// 申请年度
        Object billno = hvo.getAttributeValue("billno");// 三级项目编码
        Object pjname = hvo.getAttributeValue("pjname");// 三级项目名称
        String sql = util.buildParProject(belongpro);
        parpros = (List<Map<String, Object>>)util.getBaseDAO().executeQuery(sql, new MapListProcessor());
        Map<String, Object> bj = new HashMap<>();
        bj.put("project_code", billno);
        bj.put("project_name", pjname);
        bj.put("leve", "3");
        bj.put("budstate",budstate);
        bj.put("bdef1", "Y");
        bj.put("ysyear", applyyear);
        bj.put("bdbilldate", new UFDate());
        parpros.add(bj);
        return  parpros;
    }


    /**
     * 深拷贝
     * @param object
     * @return
     * @param <T>
     * @throws BusinessException
     */
    protected  <T> T deepCopy(T object) throws BusinessException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return (T) objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new BusinessException(e.getMessage());
        }
    }

    /**
     * 项目结项
     * @param aggvo
     * @throws BusinessException
     */
    public static void UnClosinitemFourCGProject(AggPjapplVO aggvo) throws BusinessException{
        SJXMUtil util = new SJXMUtil();
        PJApplHVO hvo = aggvo.getParentVO();
        String applyyear = hvo.getApplyyear();
        PjapplPVO[] sjvos = (PjapplPVO[])aggvo.getChildren(PjapplPVO.class);
        ArrayList<String> pks = new ArrayList<>();
        if(ArrayUtils.isEmpty(sjvos)) return;
        for (PjapplPVO sjvo : sjvos) {
            String vbdef1 = sjvo.getVbdef1();
            if("".equals(vbdef1)) continue;
            pks.add(vbdef1);
        }
        if(pks.isEmpty()) return;
        AggSjxmVO[] aggSjxmVOS = util.getSjxmHVOService().listAggSjxmVOByPk(pks.toArray(new String[pks.size()]));
        for (AggSjxmVO aggSjxmVO : aggSjxmVOS) {
            SjxmBVO[] bvos = (SjxmBVO[])aggSjxmVO.getChildren(SjxmBVO.class);
            if (ArrayUtils.isEmpty(bvos)) continue;
            for (SjxmBVO bvo : bvos) {
                if (applyyear.equals(bvo.getYsyear())){
                    bvo.setAttributeValue("fourstate", "5");
                }
            }

        }
        util.getSjxmHVOService().saveAggSjxmVO(aggSjxmVOS);
    }

    /**
     * 回写四级项目 提交
     * @param vos
     * @throws BusinessException
     */
    public static void sjxmCommitHandlerAggvo(AggPjapplVO[] vos,String budstate, String fourstate,String type, Object nyear) throws BusinessException{
        SJXMUtil util = new SJXMUtil();
        if(ArrayUtils.isEmpty(vos)) return ;
        HashMap<Object, List<Map<String, Object>>> pks = new HashMap<>();
        for (AggPjapplVO vo : vos) {
            PJApplHVO hvo = vo.getParentVO();
            Object def1 = hvo.getAttributeValue("def1");
            if(!"3".equals(def1)) continue;
            Object belongpro = hvo.getAttributeValue("belongpro");// 上级项目

            Object applyyear =  "".equals(nyear) || nyear == null ? hvo.getAttributeValue("applyyear") : nyear;// 申请年度
            Object billno = hvo.getAttributeValue("billno");// 三级项目编码
            Object pjname = hvo.getAttributeValue("pjname");// 三级项目名称
            ISuperVO[] sjvos = vo.getChildren(PjapplPVO.class);
            if(ArrayUtils.isEmpty(sjvos)) continue;
            String sql = util.buildParProject(belongpro);
            List<Map<String, Object>> parpros = (List<Map<String, Object>>)util.getBaseDAO().executeQuery(sql, new MapListProcessor());
            Map<String, Object> bj = new HashMap<>();
            bj.put("project_code", billno);
            bj.put("project_name", pjname);
            bj.put("leve", "3");
            bj.put("bdef1", "Y");
            bj.put("ysyear", applyyear);
            bj.put("bdbilldate", new UFDate());
            parpros.add(bj);
            for (ISuperVO sjvo : sjvos) {
                Object vbdef1 = sjvo.getAttributeValue("vbdef1");
                pks.put(vbdef1+"_spli_"+applyyear, parpros);
            }
        }
        if(pks.isEmpty()) return ;
        AggSjxmVO[] aggSjxmVOS = util.setValueAggSjxmVO(pks,budstate,fourstate,type);
        util.getSjxmHVOService().saveAggSjxmVO(aggSjxmVOS);
    }
    /**
     * 回写四级项目
     * @param vos
     * @throws BusinessException
     */
    public static void sjxmUNCommitHandlerAggvo(AggPjapplVO[] vos,String budstate, String fourstate,String type, Object nyear) throws BusinessException{
        SJXMUtil util = new SJXMUtil();
        if(ArrayUtils.isEmpty(vos)) return ;
        HashMap<Object, List<Map<String, Object>>> pks = new HashMap<>();
        for (AggPjapplVO vo : vos) {
            PJApplHVO hvo = vo.getParentVO();
            Object def1 = hvo.getAttributeValue("def1");
            if(!"3".equals(def1)) continue;
            Object belongpro = hvo.getAttributeValue("belongpro");// 上级项目
            Object applyyear = hvo.getAttributeValue("applyyear");// 申请年度
            ISuperVO[] sjvos = vo.getChildren(PjapplPVO.class);
            if(ArrayUtils.isEmpty(sjvos)) continue;
            String sql = util.buildParProject(belongpro);
            List<Map<String, Object>> parpros = (List<Map<String, Object>>)util.getBaseDAO().executeQuery(sql, new MapListProcessor());
            for (Map<String, Object> m : parpros) {
                Set<String> keys = m.keySet();
                for (String key : keys) {
                    if("leve".equals(key)) continue;
                    m.put(key , "");
                }
            }
            Map<String, Object> bj = new HashMap<>();
            bj.put("project_code", "");
            bj.put("project_name", "");
            bj.put("leve", "3");
            String bdef1 = "Y";
            if("D".equals(type)){
                bdef1 = "N";
            }
            bj.put("bdef1", bdef1);
            bj.put("ysyear", applyyear);
            bj.put("bdbilldate", "");
            parpros.add(bj);
            for (ISuperVO sjvo : sjvos) {
                Object vbdef1 = sjvo.getAttributeValue("vbdef1");
                pks.put(vbdef1+"_spli_"+applyyear, parpros);
            }
        }
        if(pks.isEmpty()) return ;
        AggSjxmVO[] aggSjxmVOS = util.setValueAggSjxmVO(pks,budstate,fourstate, type);
        util.getSjxmHVOService().saveAggSjxmVO(aggSjxmVOS);
    }

    /**
     * 处理四级项目信息
     * @param pks
     * @return
     * @throws BusinessException
     */
    public AggSjxmVO[] setValueAggSjxmVO(HashMap<Object, List<Map<String, Object>>> pks , String budstate, String fourstate, String type) throws BusinessException{
        SJXMUtil util = new SJXMUtil();
        List<AggSjxmVO> listvos = new ArrayList<>();
        for (Object o : pks.keySet()) {
            List<Map<String, Object>> maps = pks.get(o);
            String[] spls = o.toString().split("_spli_");
            String pk = spls[0];
            String year = spls[1];
            for (Map<String, Object> map : maps) {
                Set<String> ps = map.keySet();
                for (String p : ps) {
                    if("ysyear".equals(p)){
                        map.put(p, year);
                    }
                }
            }
            AggSjxmVO aggvo = util.getSjxmHVOService().findAggSjxmVOByPk(pk.toString());
            if (aggvo == null) continue;
            Boolean flag = true; // 是否存在
            SjxmBVO[] bvos = (SjxmBVO[])aggvo.getChildrenVO();
            if(!ArrayUtils.isEmpty(bvos)){// 四级项目存在明细
                for (SjxmBVO bvo : bvos) {
                    String ysyear = bvo.getYsyear();
                    if(year.equals(ysyear)){
                        flag = false;
                        aggvo = util.handlerAggVO(aggvo, bvo, "U", maps, budstate,fourstate);
                        break;
                    }
                }
            }
            if(flag){
                aggvo = util.handlerAggVO(aggvo, null,type, maps, budstate,fourstate);
            }
            listvos.add(aggvo);
        }
        return listvos.isEmpty() ? null : listvos.toArray(new AggSjxmVO[0]);
    }

    /**
     * 结果集处理
     * @return
     * @throws BusinessException
     */
    protected AggSjxmVO handlerAggVO(AggSjxmVO aggvo ,SjxmBVO bvo, String type, List<Map<String, Object>> maps, String budstate, String fourstate) throws BusinessException{
        SjxmHVO hvo = aggvo.getParentVO();
        String pk_org = hvo.getPk_org();
        String pk_org_v = hvo.getPk_org_v();
        hvo.setStatus(VOStatus.UPDATED);
        if("I".equals(type)){
            SjxmBVO sjxmBVO = new SjxmBVO();
            sjxmBVO.setStatus(VOStatus.NEW);
            sjxmBVO.setAttributeValue("rowno",10);
            sjxmBVO.setAttributeValue("pk_org",pk_org);
            sjxmBVO.setAttributeValue("pk_org_v",pk_org_v);
            if(budstate != null ){sjxmBVO.setAttributeValue("budstate",budstate);}
            if(fourstate != null ){sjxmBVO.setAttributeValue("fourstate",fourstate);}
            sjxmBVO.setAttributeValue("modifyobj","HOP");
            for (Map<String, Object> map : maps) {
                Object project_code = map.get("project_code");
                Object project_name = map.get("project_name");
                if("1".equals(map.get("leve").toString())){
                    sjxmBVO.setAttributeValue("oneprono",project_code);
                    sjxmBVO.setAttributeValue("oneproname",project_name);
                }else if("2".equals(map.get("leve").toString())){
                    sjxmBVO.setAttributeValue("twoprono",project_code);
                    sjxmBVO.setAttributeValue("twoproname",project_name);
                }else if("3".equals(map.get("leve").toString())){
                    sjxmBVO.setAttributeValue("threeprono",project_code);
                    sjxmBVO.setAttributeValue("threeproname",project_name);
                    sjxmBVO.setAttributeValue("bdbilldate",map.get("bdbilldate"));
                    sjxmBVO.setAttributeValue("ysyear",map.get("ysyear"));
                    sjxmBVO.setAttributeValue("bdef1",map.get("bdef1"));
                }
                if(map.get("budstate") != null ){
                    sjxmBVO.setAttributeValue("budstate",map.get("budstate"));
                }
                if(map.get("fourstate") != null ){
                    sjxmBVO.setAttributeValue("fourstate",map.get("fourstate") );
                }
            }
            aggvo.setChildrenVO(new SjxmBVO[]{sjxmBVO});
        }else{
            bvo.setStatus(VOStatus.UPDATED);
            bvo.setAttributeValue("pk_org",pk_org);
            bvo.setAttributeValue("pk_org_v",pk_org_v);
            if(budstate != null ){bvo.setAttributeValue("budstate",budstate);}
            if(fourstate != null){bvo.setAttributeValue("fourstate",fourstate);}
            bvo.setAttributeValue("modifyobj","HOP");
            for (Map<String, Object> map : maps) {
                if(map.get("budstate") != null){bvo.setAttributeValue("budstate",map.get("budstate") );}
                if(map.get("fourstate") != null){bvo.setAttributeValue("fourstate",map.get("fourstate"));}
                Object project_code = map.get("project_code");
                Object project_name = map.get("project_name");
                if(map.get("leve") == null){
                    bvo.setAttributeValue("bdbilldate",map.get("bdbilldate"));
                    bvo.setAttributeValue("threeproysmoney",map.get("threeproysmoney"));
                    bvo.setAttributeValue("threeprotzmoney",map.get("threeprotzmoney"));
                    bvo.setAttributeValue("kymoney",map.get("kymoney"));
                    bvo.setAttributeValue("threeprofinysmoney",map.get("threeprofinysmoney"));
                }else if("1".equals(map.get("leve").toString())){
                    bvo.setAttributeValue("oneprono",project_code);
                    bvo.setAttributeValue("oneproname",project_name);
                }else if("2".equals(map.get("leve").toString())){
                    bvo.setAttributeValue("twoprono",project_code);
                    bvo.setAttributeValue("twoproname",project_name);
                }else if("3".equals(map.get("leve").toString())){
                    bvo.setAttributeValue("threeprono",project_code);
                    bvo.setAttributeValue("threeproname",project_name);
                    bvo.setAttributeValue("bdbilldate","".equals(map.get("bdbilldate")) ? null : map.get("bdbilldate"));
                    bvo.setAttributeValue("ysyear",map.get("ysyear"));
                    bvo.setAttributeValue("bdef1",map.get("bdef1"));
                }
                if("jsd".equals(map.get("jsd")+"")){
                    bvo.setAttributeValue("bdef1","N");
                    bvo.setAttributeValue("oneprono","");
                    bvo.setAttributeValue("oneproname","");
                    bvo.setAttributeValue("twoprono","");
                    bvo.setAttributeValue("twoproname","");
                    bvo.setAttributeValue("threeprono","");
                    bvo.setAttributeValue("threeproname","");
                    bvo.setAttributeValue("budstate",2);
                    bvo.setAttributeValue("fourstate",1);
                    bvo.setAttributeValue("kymoney",new UFDouble(0));
                    bvo.setAttributeValue("threeprofinysmoney",new UFDouble(0));
                    bvo.setAttributeValue("threeprotzmoney",new UFDouble(0));
                    bvo.setAttributeValue("threeproysmoney",new UFDouble(0));
                    bvo.setAttributeValue("bdbilldate", null);
                    bvo.setAttributeValue("modifyobj","HOP");
                }
            }

        }
        return aggvo;
    }

    /**
     * 查询上级项目级次
     * @param belongpro
     * @return
     * @throws BusinessException
     */
    protected String buildParProject(Object belongpro) throws BusinessException{
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT project_code, project_name , def1 leve " );
        sql.append("FROM bd_project " );
        sql.append("START WITH pk_project = '"+belongpro+"' " );
        sql.append("CONNECT BY PRIOR pk_parentpro = pk_project " );
        sql.append("ORDER BY project_code, project_name ");
        return sql.toString();
    }

    /**
     * 构建审批通过的预算编制查询
     * @param nyear           预算年度
     * @param pk_budgetdept   编报科室
     * @param pk_project      三级项目申请单
     * @return
     * @throws BusinessException
     */
    protected String buildBudgetMakeSQL (Object nyear,Object pk_budgetdept, Object pk_project) throws BusinessException{
        StringBuilder sql = new StringBuilder();
        sql.append(" select nvl(b.amount, 0) amount , nvl(b.amount_adjust,0) tzamount\n" );
        sql.append("  from hrpbm_budgetmake h\n" );
        sql.append(" inner join hrpbm_budgetmakedetail b on h.pk_budgetmake = b.pk_budgetmake\n" );
        sql.append(" where h.dr = 0\n" );
        sql.append("   and b.dr = 0\n" );
        sql.append("   and h.taskstate = 99\n" );
        sql.append("   and b.nyear = '"+nyear+"'\n" );
//        sql.append("   and b.pk_budgetdept = '"+pk_budgetdept+"'\n" );
        sql.append("   and b.pk_project = '"+pk_project+"'\n ");
        return sql.toString();
    }

    public ISjxmHVOService getSjxmHVOService() {
        if(null == this.sjxmHVOService){
            this.sjxmHVOService = NCLocator.getInstance().lookup(ISjxmHVOService.class);
        }
        return this.sjxmHVOService;
    }

    public BaseDAO getBaseDAO() {
        if(null == this.baseDAO){
            this.baseDAO = new BaseDAO();
        }
        return this.baseDAO;
    }
}
